package com.tanglon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tanglon.constants.AdminConstants;
import com.tanglon.constants.SystemConstants;
import com.tanglon.domain.ResponseResult;
import com.tanglon.domain.dto.AddCategoryDTO;
import com.tanglon.domain.entity.Article;
import com.tanglon.domain.entity.Category;
import com.tanglon.domain.vo.CategoryVo;
import com.tanglon.domain.vo.CategoryVo2;
import com.tanglon.domain.vo.PageVo;
import com.tanglon.enums.AppHttpCodeEnum;
import com.tanglon.mapper.CategoryMapper;
import com.tanglon.service.ArticleService;
import com.tanglon.service.CategoryService;
import com.tanglon.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2025-08-07 11:52:19
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;
    /**
     * 获取文章分类列表
     * @return
     */
    @Override
    public ResponseResult getCategoryList() {
        //查询文章表，状态为已发布
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus,SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleService.list(articleWrapper);
        //去重文章分类id
        Set<Long> categoryIds=articles.stream()
                        .map(article ->  article.getCategoryId())
                        .collect(Collectors.toSet());
        //查询文章分类表
        List<Category> categories = listByIds(categoryIds);
        categories=categories.stream().filter(category -> SystemConstants.CATEGORY_STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVoList=BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVoList);
    }

    /**
     * 查询所有分类接口
     * @return
     */
    @Override
    public List<CategoryVo> listAllCategory() {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getStatus, AdminConstants.STATUS_NORMAL);
        List<Category> list = list(wrapper);
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return categoryVos;
    }

    @Override
    public ResponseResult getList(Integer pageNum, Integer pageSize, String name, String status) {
        //查询条件
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Objects.nonNull(status),Category::getStatus, status);
        wrapper.like(Objects.nonNull(name),Category::getName, name);

        //分页查询
        Page<Category> page = new Page<>(pageNum, pageSize);
        page(page, wrapper);
        List<CategoryVo2> categoryVo2s = BeanCopyUtils.copyBeanList(page.getRecords(), CategoryVo2.class);

        //封装数据
        PageVo pageVo=new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setRows(categoryVo2s);
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addCategory(AddCategoryDTO addCategoryDTO) {
        Category category = BeanCopyUtils.copyBean(addCategoryDTO, Category.class);
        save(category);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCategoryById(Long id) {
        Category category = getById(id);
        CategoryVo2 categoryVo2 = BeanCopyUtils.copyBean(category, CategoryVo2.class);
        return ResponseResult.okResult(categoryVo2);
    }

    @Override
    public ResponseResult updateCategory(CategoryVo2 categoryVo2) {
        Category category = BeanCopyUtils.copyBean(categoryVo2, Category.class);
        updateById(category);
        return ResponseResult.okResult();
    }
}

